package com.hctforgreen.greenservice;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.hctforgreen.greenservice.ctr.HctController;
import com.hctforgreen.greenservice.model.ResultEntity;
import com.hctforgreen.greenservice.utils.HctConstants;
import com.hctforgreen.greenservice.utils.HctResult;
import com.hctforgreen.greenservice.utils.KeyboardUtil;
import com.hctforgreen.greenservice.utils.LoginResultStoreUtil;
import com.hctforgreen.greenservice.utils.ThemeStoreUtil;

/**
 * 提交章节意见
 * 
 * @author denghb
 * 
 */
public class SubmitAdviceActivity extends ParentActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_submit_advice);
		initSkinLayout();
		initClickEdgehideKeyboard();
		initTitleButtonBar();
		initWindow();
		initSubmitBtn();
	}

	/**
	 * 点击边缘隐藏输入法
	 */
	private void initClickEdgehideKeyboard() {
		LinearLayout rootLyt = (LinearLayout) findViewById(R.id.lyt_root);
		rootLyt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				KeyboardUtil keyboardUtil = new KeyboardUtil();
				keyboardUtil.hideSoftKeyboard(v);
			}
		});
	}

	private void initSubmitBtn() {
		final String entryTitleId = getIntent().getStringExtra(
				HctConstants.ON_ACTIVITY_KEY_ENTRY_TITLEID);
		final String bookId = getIntent().getStringExtra(
				HctConstants.ON_ACTIVITY_KEY_BOOK_ID);

		final EditText content_edit = (EditText) findViewById(R.id.content_edit);
		final Button submitBtn = (Button) findViewById(R.id.sub_btn);
		submitBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String personId = LoginResultStoreUtil
						.get(SubmitAdviceActivity.this).personId;
				String content = content_edit.getText().toString().trim();
				if (content.equals("")) {
					Toast.makeText(SubmitAdviceActivity.this,
							R.string.submit_feed_content_input_not_null_hint,
							Toast.LENGTH_SHORT).show();
					return;
				}
				if (entryTitleId != null && entryTitleId.trim().length() > 0) {
					subChapterText(submitBtn, personId, entryTitleId, content);
				} else if (bookId != null && bookId.trim().length() > 0) {
					subBookText(submitBtn, personId, bookId, content);
				}
			}
		});
	}

	private void initWindow() {
	}


	private void initTitleButtonBar() {
		final TextView titleTv = (TextView) findViewById(R.id.tv_title);
		titleTv.setText(getString(R.string.submit_chapter_advice));

		final ImageButton backBtn = (ImageButton) findViewById(R.id.btn_back);
		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

	}

	/**
	 * 提交章节反馈
	 * 
	 * @param personId
	 * @param entryTitleId
	 * @param content
	 */
	private void subChapterText(final Button sub_btn, final String personId,
			final String entryTitleId, final String content) {
		final ProgressBar loadPb = (ProgressBar) findViewById(R.id.pb_loading);
		sub_btn.setClickable(false);
		loadPb.setVisibility(View.VISIBLE);

		// MobclickAgent.onEvent(this, "Normal_feadback");
		final Handler handler = new Handler() {
			public void handleMessage(Message msg) {
				sub_btn.setClickable(true);
				loadPb.setVisibility(View.GONE);

				HctResult hctResult = new HctResult();
				switch (msg.what) {
				case HctConstants.STATUS_SUCCESS:
					hctResult = (HctResult) msg.obj;
					ResultEntity entity = (ResultEntity) hctResult.data;
					if (entity.statusCode.equals("0")) {
						Toast.makeText(SubmitAdviceActivity.this,
								R.string.add_feed_sucess_hint,
								Toast.LENGTH_SHORT).show();
						setResult(RESULT_OK, getIntent());
						finish();
					} else if (!entity.statusCode.equals("0")) {

						Toast.makeText(SubmitAdviceActivity.this, entity.msg,
								Toast.LENGTH_SHORT).show();
					}
					break;

				case HctConstants.STATUS_FAILD:
					Toast.makeText(SubmitAdviceActivity.this,
							getString(R.string.net_error_hint),
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
		};
		new Thread() {
			public void run() {
				Message message = new Message();
				try {
					HctController controller = new HctController(
							SubmitAdviceActivity.this);
					HctResult result = controller.submitAdvice(personId,
							entryTitleId, content);
					if (result.status == HctConstants.STATUS_SUCCESS) {
						message.what = result.status;
						message.obj = result;
					} else {
						message.what = HctConstants.STATUS_FAILD;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				handler.sendMessage(message);
			}
		}.start();
	}

	/**
	 * 提交书的意见
	 * 
	 * @param personId
	 * @param bookId
	 * @param content
	 */
	private void subBookText(final Button sub_btn, final String personId,
			final String bookId, final String content) {
		final ProgressBar loadPb = (ProgressBar) findViewById(R.id.pb_loading);
		sub_btn.setClickable(false);
		loadPb.setVisibility(View.VISIBLE);

		// MobclickAgent.onEvent(this, "Normal_feadback");
		final Handler handler = new Handler() {
			public void handleMessage(Message msg) {
				sub_btn.setClickable(true);
				loadPb.setVisibility(View.GONE);

				HctResult hctResult = new HctResult();
				switch (msg.what) {
				case HctConstants.STATUS_SUCCESS:
					hctResult = (HctResult) msg.obj;
					ResultEntity entity = (ResultEntity) hctResult.data;
					if (entity.statusCode.equals("0")) {
						Toast.makeText(SubmitAdviceActivity.this,
								R.string.add_feed_sucess_hint,
								Toast.LENGTH_SHORT).show();
						setResult(RESULT_OK, getIntent());
						finish();
					} else if (!entity.statusCode.equals("0")) {

						Toast.makeText(SubmitAdviceActivity.this, entity.msg,
								Toast.LENGTH_SHORT).show();
					}
					break;

				case HctConstants.STATUS_FAILD:
					Toast.makeText(SubmitAdviceActivity.this,
							getString(R.string.net_error_hint),
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
		};
		new Thread() {
			public void run() {
				Message message = new Message();
				try {
					HctController controller = new HctController(
							SubmitAdviceActivity.this);
					HctResult result = controller.submitBookAdvice(personId,
							bookId, content);
					if (result.status == HctConstants.STATUS_SUCCESS) {
						message.what = result.status;
						message.obj = result;
					} else {
						message.what = HctConstants.STATUS_FAILD;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				handler.sendMessage(message);
			}
		}.start();
	}
}
